package com.ztk.threadbasic.fucai.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.ztk.threadbasic.fucai.listener.FuCaiListencer;
import com.ztk.threadbasic.fucai.mapper.FuCaiMapper;
import com.ztk.threadbasic.fucai.pojo.FuCaiHighVO;
import com.ztk.threadbasic.fucai.pojo.FuCaiVO;
import com.ztk.threadbasic.result.ExcelResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/fucai")
public class FuCaiController {

    @Autowired
    private FuCaiMapper fuCaiMapper;

    @RequestMapping("/upload")
    public ExcelResult upload(@RequestParam("file") MultipartFile file) {
        log.info("开始上传数据!!!");
        ExcelReader excelReader = null;
        try(InputStream inputStream = file.getInputStream();) {
            FuCaiListencer fuCaiListencer = new FuCaiListencer();
            excelReader = EasyExcel.read(inputStream).build();
            ReadSheet readSheet = EasyExcel.readSheet().head(FuCaiVO.class).registerReadListener(fuCaiListencer).build();
            excelReader.read(readSheet);
            List<FuCaiVO> list = fuCaiListencer.getList();

            if (!CollectionUtils.isEmpty(list)) {
                log.info("数据开始入库!!!!");
                fuCaiMapper.insertData(list);
            }
            log.info("数据入库完成!!!");
            return new ExcelResult();
        }catch (Exception e) {
            e.printStackTrace();
            log.info("上传异常!!!");
            return new ExcelResult();
        } finally {
            if (!Objects.isNull(excelReader)) {
                excelReader.finish();
            }
        }
    }

    /**
     * 规则：
     *  红球：
     *      1、同一列，该数字前三天没有出现过
     *
     *  蓝球：
     *      1、当日蓝球，在前两天的红球中没有出现过
     *
     *
     *
     * @param lastResult
     * @return
     */

    @RequestMapping("/forecast")
    public List<List<Integer>> foreCast(@RequestParam("lastResult") int lastResult) {
        // 获取高频出现的彩票结果
        FuCaiHighVO fuCaiHighVO = fuCaiMapper.selectHigh();

        // 获取近x次的福彩结果
        List<FuCaiVO> fuCaiVOS = fuCaiMapper.selectLastResult(lastResult);



        String[] redOneStr = fuCaiHighVO.getRedOne().split(",");
        List<Integer> redOne = getIntArray(redOneStr);

        String[] redTwoStr = fuCaiHighVO.getRedTwo().split(",");
        List<Integer> redTwo = getIntArray(redTwoStr);

        String[] redThreeStr = fuCaiHighVO.getRedThree().split(",");
        List<Integer> redThree = getIntArray(redThreeStr);

        String[] redFourStr = fuCaiHighVO.getRedFour().split(",");
        List<Integer> redFour = getIntArray(redFourStr);

        String[] redFiveStr = fuCaiHighVO.getRedFive().split(",");
        List<Integer> redFive = getIntArray(redFiveStr);

        String[] redSixStr = fuCaiHighVO.getRedSix().split(",");
        List<Integer> redSix = getIntArray(redSixStr);

        String[] blueStr = fuCaiHighVO.getBlue().split(",");
        List<Integer> blue = getIntArray(blueStr);


        // 剔除掉近x天出现过的数字
        for (int i = 0; i < fuCaiVOS.size(); i++) {
            FuCaiVO fuCaiVO = fuCaiVOS.get(i);
            if (redOne.contains(fuCaiVO.getRedOne())) {
                redOne.remove(Integer.valueOf(fuCaiVO.getRedOne()));
            }
            if (redTwo.contains(fuCaiVO.getRedTwo())) {
                redTwo.remove(Integer.valueOf(fuCaiVO.getRedTwo()));
            }
            if (redThree.contains(fuCaiVO.getRedThree())) {
                redThree.remove(Integer.valueOf(fuCaiVO.getRedThree()));
            }
            if (redFour.contains(fuCaiVO.getRedFour())) {
                redFour.remove(Integer.valueOf(fuCaiVO.getRedFour()));
            }
            if (redFive.contains(fuCaiVO.getRedFive())) {
                redFive.remove(Integer.valueOf(fuCaiVO.getRedFive()));
            }
            if (redSix.contains(fuCaiVO.getRedSix())) {
                redSix.remove(Integer.valueOf(fuCaiVO.getRedSix()));
            }
        }

        redOne.forEach(one -> System.out.print(one + " "));
        System.out.println();
        redTwo.forEach(two -> System.out.print(two + " "));
        System.out.println();
        redThree.forEach(three -> System.out.print(three + " "));
        System.out.println();
        redFour.forEach(four -> System.out.print(four + " "));
        System.out.println();
        redFive.forEach(five -> System.out.print(five + " "));
        System.out.println();
        redSix.forEach(six -> System.out.print(six + " "));
        System.out.println();
        blue.forEach(bluex -> System.out.print(bluex + " "));
        System.out.println();

        List<List<Integer>> totalList = new ArrayList<>();

        totalList.add(redOne);
        totalList.add(redTwo);
        totalList.add(redThree);
        totalList.add(redFour);
        totalList.add(redFive);
        totalList.add(redSix);
        totalList.add(blue);


        for (int i = 0; i < redOne.size(); i++) {
            Integer one2 = redOne.get(i);

            for (int two = 0; two < redTwo.size(); two++) {
                Integer two2 = redTwo.get(two);

                for (int three = 0; three < redThree.size(); three++) {
                    Integer three2 = redThree.get(three);

                    for (int four = 0; four < redFour.size(); four++) {
                        Integer four2 = redFour.get(four);

                        for (int five = 0; five < redFive.size(); five++) {
                            Integer five2 = redFive.get(five);

                            for (int six = 0; six < redSix.size(); six++) {
                                Integer six2 = redSix.get(six);

                                for (int blue3 = 0; blue3 < blue.size(); blue3++) {
                                    List<Integer> inList = new ArrayList<>();
                                    Integer blue2 = blue.get(blue3);
                                    inList.add(one2);
                                    inList.add(two2);
                                    inList.add(three2);
                                    inList.add(four2);
                                    inList.add(five2);
                                    inList.add(six2);
                                    inList.add(blue2);
                                    totalList.add(inList);
                                }
                            }
                        }
                    }
                }
            }
        }


        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            System.out.println();
            int index = random.nextInt(totalList.size());
            List<Integer> integers = totalList.get(index);
            integers.forEach(ran -> System.out.print(ran + " "));
        }



        return totalList;
    }


    private List<Integer> getIntArray(String[] str) {
        List<Integer> list = new ArrayList<>(str.length);
        for (int i = 0; i < str.length; i++) {
            list.add(Integer.parseInt(str[i]));
        }
        return list;
    }
}
